﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace Server
{
    public partial class SendFile : Form
    {
        public event GetFilePathEventHandler OnGetFilePath = null;
        private Thread t = null;

        public SendFile()
        {
            InitializeComponent();            
        }

        private void ShowOpenFileDialog()
        {
            try
            {
                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.tbxPath.Text = this.openFileDialog1.FileName;
                }
                if (this.t != null)
                {
                    this.t.Abort();
                }
            }
            catch
            { }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            this.t = new Thread(new ThreadStart(ShowOpenFileDialog));
            t.SetApartmentState(ApartmentState.STA);
            this.t.Start();            
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                FileStream fs = new FileStream(this.openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                if (this.OnGetFilePath != null)
                {
                    this.OnGetFilePath(this, new FileInfoEventArgs(this.openFileDialog1.FileName, (int)fs.Length));
                }
            }
            catch
            { }
        }

        private void SendFile_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.t != null)
            {
                this.t.Abort();
            }
        }
    }
}
